package com.mobimap.nhom9.gui;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
//import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
//import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
import android.widget.FrameLayout.LayoutParams;

public class Tim_Dia_Chi extends Activity{
	private static final int COL = 3;
	private static final int ROW = 4;

	private float mx, my;
	private float vp_x, vp_y;
	private float curX, curY;
	private int imgcount_x;
	private int imgcount_y;
	private float curvp_x, curvp_y;
	private ImageView[][] list;
	private RelativeLayout[][] newlist;
	private Button bt_search;
	private EditText txtKeyword;
	private ScrollView vScroll;
	private HorizontalScrollView hScroll;
	
	private Context mContext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tim_dia_chi);
		curvp_x = 0;
		curvp_y = 0;
		
		vScroll = (ScrollView) findViewById(R.id.vScroll);
		hScroll = (HorizontalScrollView) findViewById(R.id.hScroll);
		vScroll.setVerticalScrollBarEnabled(false);
		vScroll.setHorizontalScrollBarEnabled(false);
		hScroll.setVerticalScrollBarEnabled(false);
		hScroll.setHorizontalScrollBarEnabled(false);

		this.mContext = this;
		this.txtKeyword = (EditText) this.findViewById(R.id.et_TimDiaChi);
		this.txtKeyword.setText("hoang van thu");
		
		bt_search = (Button) findViewById(R.id.btn_Tim_TimDiaChi);
		bt_search.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				String keywork = txtKeyword.getText().toString();
				
				Bundle bundle = new Bundle();
				bundle.putString("search_type", "dia_diem");
				bundle.putString("keywork", keywork);
				
				Intent intent = new Intent(mContext, PlaceResultActivity.class);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		list = new ImageView[ROW][COL];
		newlist = new RelativeLayout[ROW][COL];

		for (int i = 0; i < ROW; i++) {
			for (int j = 0; j < COL; j++) {
				list[i][j] = new ImageView(this);
				String uri = "drawable/a0" + String.valueOf(i) + "0"
						+ String.valueOf(j);
				int imageResource = getResources().getIdentifier(uri, null,
						getPackageName());
				LayoutParams params = new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
						Gravity.NO_GRAVITY);
				BitmapFactory.Options op = new BitmapFactory.Options();
				op.inDither = false;
				op.inPurgeable = true;
				op.inInputShareable = true;
				op.inTempStorage = new byte [32*1014];
				list[i][j].setImageBitmap(BitmapFactory.decodeResource(
						this.getResources(), imageResource,op));
				System.gc();
				params.setMargins(j * 256, i * 256, 0, 0);
				list[i][j].setLayoutParams(params);
				
				newlist[i][j] = new RelativeLayout(this);
				newlist[i][j].addView(list[i][j]);
			
			}
		}	
		TableLayout table = new TableLayout(this);
		table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		for (int i = 0; i < ROW; i++) {
			TableRow row = new TableRow(this);
			row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
			for (int j = 0; j < COL; j++) {
				row.addView(newlist[i][j],
						new TableRow.LayoutParams(j));
			}
			table.addView(row, new TableRow.LayoutParams(i));
		}
		hScroll.addView(table);
		hScroll.post(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				vScroll.scrollTo(3*256, 4*256);
				hScroll.scrollTo(3*256, 4*256);
			}
		});
    }
    @Override
	public boolean onTouchEvent(MotionEvent event) {
		float curX, curY;

		switch (event.getAction()) {

		case MotionEvent.ACTION_DOWN:
			mx = event.getX();
			my = event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			curX = event.getX();
			curY = event.getY();
			vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
			hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
			curvp_x = hScroll.getScrollX();
			curvp_y = vScroll.getScrollY();
			//if(curvp_y - vp_y > 0&&(curvp_y > 256*(COL*6)))
			mx = curX;
			my = curY;
			break;
		case MotionEvent.ACTION_UP:
			curX = event.getX();
			curY = event.getY();
			vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
			hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
			break;
		}

		return true;
	}
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main,menu);
		return true;
	}
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId())
    	{
    		case R.id.menu_home:
    			Intent it_home = new Intent(getApplicationContext(),MainActivity.class);
    			startActivity(it_home);
    			return true;
    		case R.id.menu_timDiaChi:
    			Intent it_timDiaChi = new Intent(getApplicationContext(),Tim_Dia_Chi.class);
    			startActivity(it_timDiaChi);
    			this.finish();
    			return true;
    		case R.id.menu_timDichVu:
    			Intent it_timDichVu = new Intent(getApplicationContext(),Tim_Dich_Vu.class);
    			startActivity(it_timDichVu);
    			return true;
    		case R.id.menu_timDuong:
    			Intent it_timDuong = new Intent(getApplicationContext(),Tim_Duong.class);
    			startActivity(it_timDuong);
    			return true;
    		case R.id.menu_Exit:
    			finish();
    			return true;
			default:
				return super.onOptionsItemSelected(item);
    	}
    	
    }
	
	
}
